import { LoggingEvent, Layout } from "log4js"
import prisma, { LogLevel } from "@/common/prisma"

export const mysqlAppender = {
    configure: (param: { config: { level: string } }) => {
        const appender = async (loggingEvent: LoggingEvent) => {
            console.log(`${loggingEvent.level.levelStr}: ${loggingEvent.level.level}`)
            if (loggingEvent.level.isGreaterThanOrEqualTo(param.config.level)) {
                await prisma.log.create({
                    data: {
                        level: loggingEvent.level.levelStr.toUpperCase() as LogLevel,
                        message: loggingEvent.data.join(","),
                        category: loggingEvent.categoryName,
                        context: loggingEvent.context
                    }
                })
            }
        }
        appender.shutdown = (done: () => void) => {
        }
        return appender
    }
}